package com.example.administrator.myproject

import android.os.Bundle
import android.support.v4.app.FragmentActivity
import android.support.v4.app.FragmentTransaction
import android.widget.RadioButton
import android.widget.RadioGroup
import com.example.administrator.myproject.order.CartFragment
import com.example.administrator.myproject.order.OrderFragment
import com.example.administrator.myproject.home.MainFragment
import com.example.administrator.myproject.user.InformationFragment
import com.example.administrator.myproject.user.InformationloginFragment
import com.example.administrator.myproject.user.MerchantFragment
import com.example.administrator.myproject.user.RideFragment


class MainActivity : FragmentActivity(), RadioGroup.OnCheckedChangeListener {
    private var radioGroup: RadioGroup? = null
    private var radioButton1: RadioButton? = null
    private var radioButton2: RadioButton? = null
    private var radioButton3: RadioButton? = null
//    private var homeFragment: HomeFragment? = null
    private var MainFragment: MainFragment? = null
    private var cartFragment: CartFragment? = null
    private var merchantFragment: MerchantFragment? = null
    private var rideFragment: RideFragment? = null

    private var orderFragment: OrderFragment? = null

    private var informationFragment: InformationFragment? = null
    private var informationloginFragment: InformationloginFragment? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        radioGroup = findViewById(R.id.rg_main)
        radioButton1 = findViewById(R.id.rb_main)
        radioButton2 = findViewById(R.id.rb_find)
        radioButton3 = findViewById(R.id.rb_my)
        radioGroup!!.setOnCheckedChangeListener(this)
        setSelect(R.id.rb_main)


    }
    fun hideAllFragment(transaction: FragmentTransaction) {
//        if (homeFragment != null) {
//            transaction.hide(homeFragment!!)
//        }

        if (MainFragment != null) {
            transaction.hide(MainFragment!!)
        }
        if (orderFragment != null) {
            transaction.hide(orderFragment!!)
        }
        if (cartFragment != null) {
            transaction.hide(cartFragment!!)
        }
        if (informationFragment != null) {
            transaction.hide(informationFragment!!)
        }
        if (informationloginFragment != null) {
            transaction.hide(informationloginFragment!!)
        }
        if (merchantFragment!= null) {
            transaction.hide(merchantFragment!!)
        }
        if (rideFragment!= null) {
            transaction.hide(rideFragment!!)
        }

    }

    override fun onCheckedChanged(radioGroup: RadioGroup, i: Int) {
//        var application: App = this.application as App
//        println(application.getUserid())
        setSelect(i)
    }

    private fun setSelect(i: Int) {
        val fragmentManager = supportFragmentManager.beginTransaction()
        hideAllFragment(fragmentManager)
        var application: App = this.application as App
        when (i) {

            R.id.rb_main -> if (MainFragment == null) {
                MainFragment = MainFragment()
                fragmentManager.add(R.id.fragment, MainFragment!!)
                fragmentManager.show(MainFragment!!)
                println(776)
            } else {
                fragmentManager.show(MainFragment!!)
                println(777)
            }
            R.id.rb_find -> if (application.getUserid() == null) {
                    if(orderFragment == null) {
                        orderFragment = OrderFragment()
                        fragmentManager.add(R.id.fragment, orderFragment!!)
                        fragmentManager.show(orderFragment!!)
                        println(1)
                    }
                    else {
                        fragmentManager.show(orderFragment!!)
                    }
                } else {
                    if(cartFragment == null) {
                        cartFragment = CartFragment()
                        fragmentManager.add(R.id.fragment, cartFragment!!)
                        fragmentManager.show(cartFragment!!)
                    } else {
                        fragmentManager.show(cartFragment!!)
                    }

                }




            R.id.rb_my -> if (application.getUserid() == null) {
                if (informationFragment == null) {
                    informationFragment = InformationFragment()
                    fragmentManager.add(R.id.fragment, informationFragment!!)
                    fragmentManager.show(informationFragment!!)
                } else {
                    fragmentManager.show(informationFragment!!)
                }
            } else{
                if (rideFragment == null) {
                    rideFragment = RideFragment()
                    fragmentManager.add(R.id.fragment, rideFragment!!)
                    fragmentManager.show(rideFragment!!)
                } else {
                    fragmentManager.show(rideFragment!!)
                }
            }
        }
        fragmentManager.commit()
    }
}
